home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zdevice.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  12.4 KB  |  448 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zdevice.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Device-related operators */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "ialloc.h"
  25. #include "idict.h"
  26. #include "igstate.h"
  27. #include "iname.h"
  28. #include "interp.h"
  29. #include "iparam.h"
  30. #include "ivmspace.h"
  31. #include "gsmatrix.h"
  32. #include "gsstate.h"
  33. #include "gxdevice.h"
  34. #include "gxgetbit.h"
  35. #include "store.h"
  36.  
  37. /* <device> <keep_open> .copydevice2 <newdevice> */
  38. private int
  39. zcopydevice2(i_ctx_t *i_ctx_p)
  40. {
  41.     os_ptr op = osp;
  42.     gx_device *new_dev;
  43.     int code;
  44.  
  45.     check_read_type(op[-1], t_device);
  46.     check_type(*op, t_boolean);
  47.     code = gs_copydevice2(&new_dev, op[-1].value.pdevice, op->value.boolval,
  48.               imemory);
  49.     if (code < 0)
  50.     return code;
  51.     new_dev->memory = imemory;
  52.     make_tav(op - 1, t_device, icurrent_space | a_all, pdevice, new_dev);
  53.     pop(1);
  54.     return 0;
  55. }
  56.  
  57. /* - currentdevice <device> */
  58. int
  59. zcurrentdevice(i_ctx_t *i_ctx_p)
  60. {
  61.     os_ptr op = osp;
  62.     gx_device *dev = gs_currentdevice(igs);
  63.     gs_ref_memory_t *mem = (gs_ref_memory_t *) dev->memory;
  64.  
  65.     push(1);
  66.     make_tav(op, t_device,
  67.          (mem == 0 ? avm_foreign : imemory_space(mem)) | a_all,
  68.          pdevice, dev);
  69.     return 0;
  70. }
  71.  
  72. /* <device> .devicename <string> */
  73. private int
  74. zdevicename(i_ctx_t *i_ctx_p)
  75. {
  76.     os_ptr op = osp;
  77.     const char *dname;
  78.  
  79.     check_read_type(*op, t_device);
  80.     dname = op->value.pdevice->dname;
  81.     make_const_string(op, avm_foreign | a_readonly, strlen(dname),
  82.               (const byte *)dname);
  83.     return 0;
  84. }
  85.  
  86. /* - .doneshowpage - */
  87. private int
  88. zdoneshowpage(i_ctx_t *i_ctx_p)
  89. {
  90.     gx_device *dev = gs_currentdevice(igs);
  91.     gx_device *tdev = (*dev_proc(dev, get_page_device)) (dev);
  92.  
  93.     if (tdev != 0)
  94.     tdev->ShowpageCount++;
  95.     return 0;
  96. }
  97.  
  98. /* - flushpage - */
  99. int
  100. zflushpage(i_ctx_t *i_ctx_p)
  101. {
  102.     return gs_flushpage(igs);
  103. }
  104.  
  105. /* <device> <x> <y> <width> <max_height> <alpha?> <std_depth|null> <string> */
  106. /*   .getbitsrect <height> <substring> */
  107. private int
  108. zgetbitsrect(i_ctx_t *i_ctx_p)
  109. {    /*
  110.      * alpha? is 0 for no alpha, -1 for alpha first, 1 for alpha last.
  111.      * std_depth is null for native pixels, depth/component for
  112.      * standard color space.
  113.      */
  114.     os_ptr op = osp;
  115.     gx_device *dev;
  116.     gs_int_rect rect;
  117.     gs_get_bits_params_t params;
  118.     int w, h;
  119.     gs_get_bits_options_t options =
  120.     GB_ALIGN_ANY | GB_RETURN_COPY | GB_OFFSET_0 | GB_RASTER_STANDARD |
  121.     GB_PACKING_CHUNKY;
  122.     int depth;
  123.     uint raster;
  124.     int num_rows;
  125.     int code;
  126.  
  127.     check_read_type(op[-7], t_device);
  128.     dev = op[-7].value.pdevice;
  129.     check_int_leu(op[-6], dev->width);
  130.     rect.p.x = op[-6].value.intval;
  131.     check_int_leu(op[-5], dev->height);
  132.     rect.p.y = op[-5].value.intval;
  133.     check_int_leu(op[-4], dev->width);
  134.     w = op[-4].value.intval;
  135.     check_int_leu(op[-3], dev->height);
  136.     h = op[-3].value.intval;
  137.     check_type(op[-2], t_integer);
  138.     /*
  139.      * We use if/else rather than switch because the value is long,
  140.      * which is not supported as a switch value in pre-ANSI C.
  141.      */
  142.     if (op[-2].value.intval == -1)
  143.     options |= GB_ALPHA_FIRST;
  144.     else if (op[-2].value.intval == 0)
  145.     options |= GB_ALPHA_NONE;
  146.     else if (op[-2].value.intval == 1)
  147.     options |= GB_ALPHA_LAST;
  148.     else
  149.     return_error(e_rangecheck);
  150.     if (r_has_type(op - 1, t_null)) {
  151.     options |= GB_COLORS_NATIVE;
  152.     depth = dev->color_info.depth;
  153.     } else {
  154.     static const gs_get_bits_options_t depths[17] = {
  155.         0, GB_DEPTH_1, GB_DEPTH_2, 0, GB_DEPTH_4, 0, 0, 0, GB_DEPTH_8,
  156.         0, 0, 0, GB_DEPTH_12, 0, 0, 0, GB_DEPTH_16
  157.     };
  158.     gs_get_bits_options_t depth_option;
  159.     int std_depth;
  160.  
  161.     check_int_leu(op[-1], 16);
  162.     std_depth = (int)op[-1].value.intval;
  163.     depth_option = depths[std_depth];
  164.     if (depth_option == 0)
  165.         return_error(e_rangecheck);
  166.     options |= depth_option | gb_colors_for_device(dev);
  167.     depth = (dev->color_info.num_components +
  168.          (options & GB_ALPHA_NONE ? 0 : 1)) * std_depth;
  169.     }
  170.     raster = (w * depth + 7) >> 3;
  171.     check_write_type(*op, t_string);
  172.     num_rows = r_size(op) / raster;
  173.     h = min(h, num_rows);
  174.     if (h == 0)
  175.     return_error(e_rangecheck);
  176.     rect.q.x = rect.p.x + w;
  177.     rect.q.y = rect.p.y + h;
  178.     params.options = options;
  179.     params.data[0] = op->value.bytes;
  180.     code = (*dev_proc(dev, get_bits_rectangle))(dev, &rect, ¶ms, NULL);
  181.     if (code < 0)
  182.     return code;
  183.     make_int(op - 7, h);
  184.     op[-6] = *op;
  185.     r_set_size(op - 6, h * raster);
  186.     pop(6);
  187.     return 0;
  188. }
  189.  
  190. /* <int> .getdevice <device> */
  191. private int
  192. zgetdevice(i_ctx_t *i_ctx_p)
  193. {
  194.     os_ptr op = osp;
  195.     const gx_device *dev;
  196.  
  197.     check_type(*op, t_integer);
  198.     if (op->value.intval != (int)(op->value.intval))
  199.     return_error(e_rangecheck);    /* won't fit in an int */
  200.     dev = gs_getdevice((int)(op->value.intval));
  201.     if (dev == 0)        /* index out of range */
  202.     return_error(e_rangecheck);
  203.     /* Device prototypes are read-only; */
  204.     /* the cast is logically unnecessary. */
  205.     make_tav(op, t_device, avm_foreign | a_readonly, pdevice,
  206.          (gx_device *) dev);
  207.     return 0;
  208. }
  209.  
  210. /* Common functionality of zgethardwareparms & zgetdeviceparams */
  211. private int
  212. zget_device_params(i_ctx_t *i_ctx_p, bool is_hardware)
  213. {
  214.     os_ptr op = osp;
  215.     ref rkeys;
  216.     gx_device *dev;
  217.     stack_param_list list;
  218.     int code;
  219.     ref *pmark;
  220.  
  221.     check_read_type(op[-1], t_device);
  222.     rkeys = *op;
  223.     dev = op[-1].value.pdevice;
  224.     pop(1);
  225.     stack_param_list_write(&list, &o_stack, &rkeys, iimemory);
  226.     code = gs_get_device_or_hardware_params(dev, (gs_param_list *) & list,
  227.                         is_hardware);
  228.     if (code < 0) {
  229.     /* We have to put back the top argument. */
  230.     if (list.count > 0)
  231.         ref_stack_pop(&o_stack, list.count * 2 - 1);
  232.     else
  233.         ref_stack_push(&o_stack, 1);
  234.     *osp = rkeys;
  235.     return code;
  236.     }
  237.     pmark = ref_stack_index(&o_stack, list.count * 2);
  238.     make_mark(pmark);
  239.     return 0;
  240. }
  241. /* <device> <key_dict|null> .getdeviceparams <mark> <name> <value> ... */
  242. private int
  243. zgetdeviceparams(i_ctx_t *i_ctx_p)
  244. {
  245.     return zget_device_params(i_ctx_p, false);
  246. }
  247. /* <device> <key_dict|null> .gethardwareparams <mark> <name> <value> ... */
  248. private int
  249. zgethardwareparams(i_ctx_t *i_ctx_p)
  250. {
  251.     return zget_device_params(i_ctx_p, true);
  252. }
  253.  
  254. /* <matrix> <width> <height> <palette> <word?> makewordimagedevice <device> */
  255. private int
  256. zmakewordimagedevice(i_ctx_t *i_ctx_p)
  257. {
  258.     os_ptr op = osp;
  259.     os_ptr op1 = op - 1;
  260.     gs_matrix imat;
  261.     gx_device *new_dev;
  262.     const byte *colors;
  263.     int colors_size;
  264.     int code;
  265.  
  266.     check_int_leu(op[-3], max_uint >> 1);    /* width */
  267.     check_int_leu(op[-2], max_uint >> 1);    /* height */
  268.     check_type(*op, t_boolean);
  269.     if (r_has_type(op1, t_null)) {    /* true color */
  270.     colors = 0;
  271.     colors_size = -24;    /* 24-bit true color */
  272.     } else if (r_has_type(op1, t_integer)) {
  273.     /*
  274.      * We use if/else rather than switch because the value is long,
  275.      * which is not supported as a switch value in pre-ANSI C.
  276.      */
  277.     if (op1->value.intval != 16 && op1->value.intval != 24 &&
  278.         op1->value.intval != 32
  279.         )
  280.         return_error(e_rangecheck);
  281.     colors = 0;
  282.     colors_size = -op1->value.intval;
  283.     } else {
  284.     check_type(*op1, t_string);    /* palette */
  285.     if (r_size(op1) > 3 * 256)
  286.         return_error(e_rangecheck);
  287.     colors = op1->value.bytes;
  288.     colors_size = r_size(op1);
  289.     }
  290.     if ((code = read_matrix(op - 4, &imat)) < 0)
  291.     return code;
  292.     /* Everything OK, create device */
  293.     code = gs_makewordimagedevice(&new_dev, &imat,
  294.                   (int)op[-3].value.intval,
  295.                   (int)op[-2].value.intval,
  296.                   colors, colors_size,
  297.                   op->value.boolval, true, imemory);
  298.     if (code == 0) {
  299.     new_dev->memory = imemory;
  300.     make_tav(op - 4, t_device, imemory_space(iimemory) | a_all,
  301.          pdevice, new_dev);
  302.     pop(4);
  303.     }
  304.     return code;
  305. }
  306.  
  307. /* - nulldevice - */
  308. /* Note that nulldevice clears the current pagedevice. */
  309. private int
  310. znulldevice(i_ctx_t *i_ctx_p)
  311. {
  312.     gs_nulldevice(igs);
  313.     clear_pagedevice(istate);
  314.     return 0;
  315. }
  316.  
  317. /* <num_copies> <flush_bool> .outputpage - */
  318. private int
  319. zoutputpage(i_ctx_t *i_ctx_p)
  320. {
  321.     os_ptr op = osp;
  322.     int code;
  323.  
  324.     check_type(op[-1], t_integer);
  325.     check_type(*op, t_boolean);
  326.     code = gs_output_page(igs, (int)op[-1].value.intval,
  327.               op->value.boolval);
  328.     if (code < 0)
  329.     return code;
  330.     pop(2);
  331.     return 0;
  332. }
  333.  
  334. /* <device> <policy_dict|null> <require_all> <mark> <name> <value> ... */
  335. /*      .putdeviceparams */
  336. /*   (on success) <device> <eraseflag> */
  337. /*   (on failure) <device> <policy_dict|null> <require_all> <mark> */
  338. /*       <name1> <error1> ... */
  339. /* For a key that simply was not recognized, if require_all is true, */
  340. /* the result will be an /undefined error; if require_all is false, */
  341. /* the key will be ignored. */
  342. /* Note that .putdeviceparams clears the current pagedevice. */
  343. private int
  344. zputdeviceparams(i_ctx_t *i_ctx_p)
  345. {
  346.     uint count = ref_stack_counttomark(&o_stack);
  347.     ref *prequire_all;
  348.     ref *ppolicy;
  349.     ref *pdev;
  350.     gx_device *dev;
  351.     stack_param_list list;
  352.     int code;
  353.     int old_width, old_height;
  354.     int i, dest;
  355.  
  356.     if (count == 0)
  357.     return_error(e_unmatchedmark);
  358.     prequire_all = ref_stack_index(&o_stack, count);
  359.     ppolicy = ref_stack_index(&o_stack, count + 1);
  360.     pdev = ref_stack_index(&o_stack, count + 2);
  361.     if (pdev == 0)
  362.     return_error(e_stackunderflow);
  363.     check_type_only(*prequire_all, t_boolean);
  364.     check_write_type_only(*pdev, t_device);
  365.     dev = pdev->value.pdevice;
  366.     code = stack_param_list_read(&list, &o_stack, 0, ppolicy,
  367.                  prequire_all->value.boolval, iimemory);
  368.     if (code < 0)
  369.     return code;
  370.     old_width = dev->width;
  371.     old_height = dev->height;
  372.     code = gs_putdeviceparams(dev, (gs_param_list *) & list);
  373.     /* Check for names that were undefined or caused errors. */
  374.     for (dest = count - 2, i = 0; i < count >> 1; i++)
  375.     if (list.results[i] < 0) {
  376.         *ref_stack_index(&o_stack, dest) =
  377.         *ref_stack_index(&o_stack, count - (i << 1) - 2);
  378.         gs_errorname(i_ctx_p, list.results[i],
  379.              ref_stack_index(&o_stack, dest - 1));
  380.         dest -= 2;
  381.     }
  382.     iparam_list_release(&list);
  383.     if (code < 0) {        /* There were errors reported. */
  384.     ref_stack_pop(&o_stack, dest + 1);
  385.     return 0;
  386.     }
  387.     if (code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height))) {
  388.     /*
  389.      * The device was open and is now closed, or its dimensions have
  390.      * changed.  If it was the current device, call setdevice to
  391.      * reinstall it and erase the page.
  392.      */
  393.     /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
  394.     if (gs_currentdevice(igs) == dev) {
  395.         bool was_open = dev->is_open;
  396.  
  397.         code = gs_setdevice_no_erase(igs, dev);
  398.         /* If the device wasn't closed, setdevice won't erase the page. */
  399.         if (was_open && code >= 0)
  400.         code = 1;
  401.     }
  402.     }
  403.     if (code < 0)
  404.     return code;
  405.     ref_stack_pop(&o_stack, count + 1);
  406.     make_bool(osp, code);
  407.     clear_pagedevice(istate);
  408.     return 0;
  409. }
  410.  
  411. /* <device> .setdevice <eraseflag> */
  412. /* Note that .setdevice clears the current pagedevice. */
  413. int
  414. zsetdevice(i_ctx_t *i_ctx_p)
  415. {
  416.     os_ptr op = osp;
  417.     int code;
  418.  
  419.     check_write_type(*op, t_device);
  420.     code = gs_setdevice_no_erase(igs, op->value.pdevice);
  421.     if (code < 0)
  422.     return code;
  423.     make_bool(op, code != 0);    /* erase page if 1 */
  424.     clear_pagedevice(istate);
  425.     return code;
  426. }
  427.  
  428. /* ------ Initialization procedure ------ */
  429.  
  430. const op_def zdevice_op_defs[] =
  431. {
  432.     {"1.copydevice2", zcopydevice2},
  433.     {"0currentdevice", zcurrentdevice},
  434.     {"1.devicename", zdevicename},
  435.     {"0.doneshowpage", zdoneshowpage},
  436.     {"0flushpage", zflushpage},
  437.     {"7.getbitsrect", zgetbitsrect},
  438.     {"1.getdevice", zgetdevice},
  439.     {"2.getdeviceparams", zgetdeviceparams},
  440.     {"2.gethardwareparams", zgethardwareparams},
  441.     {"5makewordimagedevice", zmakewordimagedevice},
  442.     {"0nulldevice", znulldevice},
  443.     {"2.outputpage", zoutputpage},
  444.     {"3.putdeviceparams", zputdeviceparams},
  445.     {"1.setdevice", zsetdevice},
  446.     op_def_end(0)
  447. };
  448.